import { defineStore } from "pinia";
import { Message } from "@arco-design/web-vue";
import * as orderApi from "@/api/order";

export const useOrderStore = defineStore("order", () => {
  async function getOrderList(param?: any) {
    const response = await orderApi.getOrderList(param);
    if (response.code === 20000) {
      return response.data.orders;
    }
    return [];
  }

  async function saveOrder(param?: any) {
    const apiCall = param.id ? orderApi.updateOrder : orderApi.createOrder;
    const response = await apiCall({ ...param });
    if (response.code === 20000) {
      Message.success(param.id ? "更新成功" : "新增成功");
    }
  }

  async function deleteOrder(param?: any) {
    const response = await orderApi.deleteOrder(param);
    if (response.code === 20000) {
      Message.success("移除成功");
    }
  }

  async function getOrderDetail(param?: any) {
    const response = await orderApi.getOrderDetail(param);
    if (response.code === 20000) {
      return response.data.order;
    }
    return {};
  }

  return { getOrderList, saveOrder, deleteOrder, getOrderDetail };
});
